package com.thinvent.nj.ais.service.impl;

import com.thinvent.nj.common.exception.ServiceException;
import com.thinvent.nj.common.page.Page;
import com.thinvent.nj.common.page.QueryParameter;
import com.thinvent.nj.common.util.StringUtil;
import com.thinvent.nj.mybatis.service.impl.BaseCURDServiceImpl;
import com.thinvent.nj.ais.entity.Resource;
import com.thinvent.nj.ais.entity.Role;
import com.thinvent.nj.ais.entity.User;
import com.thinvent.nj.ais.mapper.RoleMapper;
import com.thinvent.nj.ais.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 角色Service
 * @author liupj
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class RoleServiceImpl extends BaseCURDServiceImpl<Role, String> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @CacheEvict(cacheNames = "sys", allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveRoleResources(String roleId, List<Resource> resources) {
        checkRoleNotNull(roleId);

        // 删除角色资源关联关系
        roleMapper.delRoleResourceList(roleId);

        // 保存角色资源关联关系
        if (resources.size() > 0) {
            roleMapper.insertRoleResourceList(roleId, resources);
        }

    }

    @CacheEvict(cacheNames = "sys", allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveRoleUsers(String roleId, List<User> users) {
        checkRoleNotNull(roleId);

        // 删除角色资源关联关系
        roleMapper.delRoleUserList(roleId);

        // 保存角色资源关联关系

        if (users.size() > 0) {
            roleMapper.insertRoleUserList(roleId, users);
        }

    }

    private void checkRoleNotNull(String roleId) {
        if (StringUtil.isNullOrEmpty(roleId)) {
            throw new ServiceException("roleId cannot be null or empty.");
        }

        Role role = get(roleId);

        if (role == null) {
            throw new ServiceException("role cannot be null or empty, current roleId = " + roleId);
        }
    }


    @Cacheable(cacheNames = "sys", keyGenerator = "keyGenerator")
    @Override
    public List<Role> findListByOrgId(String orgId) {
        return roleMapper.findListByOrgId(orgId);
    }

    @Cacheable(cacheNames = "sys", keyGenerator = "keyGenerator")
    @Override
    public List<Role> findListByUserId(String userId) {
        return roleMapper.findListByUserId(userId);
    }

    @Cacheable(cacheNames = "sys", keyGenerator = "keyGenerator")
    @Override
    public Role get(String s) {
        return super.get(s);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "sys", allEntries = true)
    @Override
    public void delete(String s) {
        super.delete(s);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "sys", allEntries = true)
    @Override
    public void update(Role role) {
        super.update(role);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "sys", allEntries = true)
    @Override
    public void insert(Role role) {
        super.insert(role);
    }

    @Cacheable(cacheNames = "sys", keyGenerator = "keyGenerator")
    @Override
    public List<Role> findList(Map<String, Object> params) {
        return super.findList(params);
    }

    @Cacheable(cacheNames = "sys", keyGenerator = "keyGenerator")
    @Override
    public Page<Role> findPage(QueryParameter queryParameter, Map<String, Object> params) {
        return super.findPage(queryParameter, params);
    }
}
